package com.example.logindb.controller;



import com.example.logindb.dto.ResponseMessage;
import com.example.logindb.dto.UserDTO;
import com.example.logindb.entity.User;
import com.example.logindb.sevices.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserService userService;


    //增加
    @PostMapping
    public ResponseMessage<User> add(@RequestBody UserDTO user)
    {

        if ( !userService.isExists(user.getUsername()) ) {
            userService.saveUser(user);
            //YYL检查用
            System.out.println("即将进入到保存阶段");
            return ResponseMessage.success(null);
        }
        else {
            System.out.println("该账号已存在");
            throw new IllegalArgumentException("该手机账号已绑定");

        }


    }


    //按照手机号进行查询
    @GetMapping("/s/{user_phone}")
    public ResponseMessage getUserbyphone(@PathVariable("user_phone") String user_phone)
    {
        User usernew= userService.getUserbyphone(user_phone);
        return ResponseMessage.success(usernew);
    }


    //查询
    @GetMapping("/{user_id}")
    public ResponseMessage Getuser(@PathVariable("user_id") Integer user_id)
    {
        User user = userService.getUser(user_id);
        return ResponseMessage.success(user);
    }

    //根据id查用户名
    @GetMapping("/username")
    public String GetUsername(@RequestParam Integer id)
    {
        User user = userService.getUser(id);
        user.setPassword(null);
        return user.getName();
    }

    //志愿者端根据id查盲人电话
    @GetMapping("blindphone")
    public String GetBlindPhone(@RequestParam Integer id)
    {
        User user = userService.getUser(id);
        return user.getUsername();
    }


    //修改
    @PutMapping
    public ResponseMessage edit(@RequestBody UserDTO user)
    {
        User usernew=userService.edit(user);

        return ResponseMessage.success(usernew);
    }


}
